<template>
  <a-select
    show-search
    option-filter-prop="children"
    :placeholder="$t('pleaseSelect') + $t('strategy').toLowerCase()"
    allow-clear
    :disabled="readonly"
    v-model="selectedValue"
  >
    <a-select-option
      v-for="(item, index) in strategyOptions"
      :key="'strategyOptions-'+item.id"
      :value="item.id"
      :title="item.name"
    >{{ item.name }} 【{{StrategyJs.getTestTypeDesc(item.testType)}}】</a-select-option>
  </a-select>
</template>
<script>
// 策略下接数据(升级,条件)
import strategyApi from '@/api/modules/strategy'
import StrategyJs from '@views/strategy/strategy'
export default {
  components: {
  },
  props: {
    versionType: {
      type: String,
      default: ''
    },
    upgradeTriggerType: {
      type: String,
      default: ''
    },
    // 升级策略1,条件策略2
    type: {
      type: [Number, String, Object],
      default () {
        return null
      }
    },
    time: {
      type: [Number, Object],
      default () {
        return null
      }
    },
    defaultStrateId: {
      type: [String, undefined],
      default: undefined
    },
    readonly: {
      type: Boolean,
      default: false
    },
    query: {
      type: Object,
      default () {
        return null
      }
    },
    getPopupContainer: {
      type: Function,
      default () {
        return document.body
      }
    }
  },
  model: {
    prop: "defaultStrateId", //绑定的值，通过父组件传递
    event: "change" //自定义事件名
  },
  computed: {
    selectedValue:{
      get() {
        return this.defaultStrateId;
      },
      set(val) {
        this.$emit('change', val);    // 触发
        this.changeOption(val)
      }
    }
  },
  data () {
    return {
      StrategyJs,
      defaultOption: '',
      strategyOptions: []
    }
  },
  watch: {
    time: {
      handler () {
        if(!this.readonly){
          this.getData()
        }
      },
      immediate: true
    },
    versionType() {
      this.selectedValue = undefined
      this.getData();
    }
  },
  created () {
    this.$store.dispatch('getVersionTypeDictCodeData')
  },
  methods: {
    getData () {
      const params = this.query ? { ...this.query } : {
        type: this.type
      }
      strategyApi.getStrategyAllData(params, this.versionType).then((res) => {
        this.strategyOptions = res.data
      })
    },
    changeOption (val) {
      this.$emit('changeoption', val)
    }
  }
}
</script>
